{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Number of Enclaves"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #depth-first-search #breadth-first-search #union-find #array #matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #深度优先搜索 #广度优先搜索 #并查集 #数组 #矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: numEnclaves"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #飞地的数量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个大小为 <code>m x n</code> 的二进制矩阵 <code>grid</code> ，其中 <code>0</code> 表示一个海洋单元格、<code>1</code> 表示一个陆地单元格。</p>\n",
    "\n",
    "<p>一次 <strong>移动</strong> 是指从一个陆地单元格走到另一个相邻（<strong>上、下、左、右</strong>）的陆地单元格或跨过 <code>grid</code> 的边界。</p>\n",
    "\n",
    "<p>返回网格中<strong> 无法 </strong>在任意次数的移动中离开网格边界的陆地单元格的数量。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/02/18/enclaves1.jpg\" style=\"height: 200px; width: 200px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>有三个 1 被 0 包围。一个 1 没有被包围，因为它在边界上。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/02/18/enclaves2.jpg\" style=\"height: 200px; width: 200px;\" />\n",
    "<pre>\n",
    "<strong>输入：</strong>grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>所有 1 都在边界上或可以到达边界。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>m == grid.length</code></li>\n",
    "\t<li><code>n == grid[i].length</code></li>\n",
    "\t<li><code>1 &lt;= m, n &lt;= 500</code></li>\n",
    "\t<li><code>grid[i][j]</code> 的值为 <code>0</code> 或 <code>1</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [number-of-enclaves](https://leetcode.cn/problems/number-of-enclaves/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [number-of-enclaves](https://leetcode.cn/problems/number-of-enclaves/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]', '[[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import deque\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, a: List[List[int]]) -> int:\n",
    "        n,m = len(a),len(a[0])\n",
    "        \n",
    "        q = deque()\n",
    "\n",
    "        for i in range(n):\n",
    "            if a[i][0]:\n",
    "                q.append((i,0))\n",
    "            if a[i][m-1]:\n",
    "                q.append((i,m-1))\n",
    "        \n",
    "        for j in range(m):\n",
    "            if a[0][j]:\n",
    "                q.append((0,j))\n",
    "            if a[n-1][j]:\n",
    "                q.append((n-1,j))\n",
    "        \n",
    "        while len(q):\n",
    "            # print(q)\n",
    "            x,y = q.popleft()\n",
    "            if a[x][y] == 0:\n",
    "                continue\n",
    "            \n",
    "            a[x][y] = 0\n",
    "\n",
    "            for dx,dy in [[0,1],[0,-1],[1,0],[-1,0]]:\n",
    "                xx=x+dx\n",
    "                yy=y+dy\n",
    "                if xx>=0 and xx<n and yy>=0 and yy<m and a[xx][yy]==1:\n",
    "                    q.append((xx,yy))\n",
    "        \n",
    "        return sum([sum(x)for x in a])\n",
    "\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, A: List[List[int]]) -> int:\n",
    "        m, n = len(A), len(A[0])\n",
    "        q = []\n",
    "        dx = [1, -1, 0, 0]\n",
    "        dy = [0, 0, 1, -1]\n",
    "        for i in range(m):\n",
    "            if A[i][0] == 1:\n",
    "                A[i][0] = 2\n",
    "            if A[i][n-1] == 1:\n",
    "                A[i][n-1] = 2\n",
    "        for i in range(n):\n",
    "            if A[0][i] == 1:\n",
    "                A[0][i] = 2\n",
    "            if A[m-1][i] == 1:\n",
    "                A[m-1][i] = 2\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if A[i][j] == 2:\n",
    "                    q.append((i, j))\n",
    "                    while len(q):\n",
    "                        Q = q.pop(0)\n",
    "                        r, c = Q[0], Q[1]\n",
    "                        for k in range(4):\n",
    "                            x = r + dx[k]\n",
    "                            y = c + dy[k]\n",
    "                            if 0 <= x < m and 0 <= y < n and A[x][y] == 1:\n",
    "                                A[x][y] = 2\n",
    "                                q.append((x, y))\n",
    "        num = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if A[i][j] == 1:\n",
    "                    num += 1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, A: List[List[int]]) -> int:\n",
    "        if not A:\n",
    "            return 0\n",
    "        l_x = len(A)\n",
    "        l_y = len(A[0])\n",
    "        ans = 0\n",
    "        def bfs(x0, y0):\n",
    "            nonlocal l_x, l_y, A\n",
    "            que = collections.deque()\n",
    "            que.append((x0, y0))\n",
    "            while que:\n",
    "                x1, y1 = que.popleft()\n",
    "                for x2, y2 in [(x1-1, y1), (x1+1, y1), (x1, y1-1), (x1, y1+1)]:\n",
    "                    if 0 <= x2 < l_x and 0 <= y2 < l_y and A[x2][y2] == 1:\n",
    "                        A[x2][y2] = 'X'\n",
    "                        que.append((x2, y2))\n",
    "        for i in [0, l_x-1]:\n",
    "            for j in range(l_y):\n",
    "                if A[i][j] == 1:\n",
    "                    A[i][j] = 'X'\n",
    "                    bfs(i, j)\n",
    "        for i in range(l_x):\n",
    "            for j in [0, l_y-1]:\n",
    "                if A[i][j] == 1:\n",
    "                    A[i][j] = 'X'\n",
    "                    bfs(i, j)\n",
    "\n",
    "        ans = 0\n",
    "        for i in range(l_x):\n",
    "            for j in range(l_y):\n",
    "                if A[i][j] == 1:\n",
    "                    ans += 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        result = 0\n",
    "        row, col = len(grid), len(grid[0])\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                if grid[i][j] == 1:\n",
    "                    cur = 1\n",
    "                    grid[i][j] = 0\n",
    "                    flag = True\n",
    "                    queue = deque([(i, j)])\n",
    "                    while queue:\n",
    "                        m, n = queue.popleft()\n",
    "                        if m in (0, row - 1) or n in (0, col - 1):\n",
    "                            flag = False\n",
    "                        for a, b in ((-1, 0), (1, 0), (0, -1), (0, 1)):\n",
    "                            x, y = m + a, n + b\n",
    "                            if 0 <= x < row and 0 <= y < col and grid[x][y] == 1:\n",
    "                                grid[x][y] = 0\n",
    "                                queue.append((x, y))\n",
    "                                cur += 1\n",
    "                    if flag:\n",
    "                        result += cur\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        direction = [[-1,0], [1,0], [0,-1], [0,1]]\n",
    "        visited = [[False] * len(grid[0]) for _ in range(len(grid))]\n",
    "        def bfs(x, y, grid, visited):\n",
    "            queue = [(x, y)]\n",
    "            visited[x][y] = True\n",
    "            while queue:\n",
    "                x, y = queue.pop(0)\n",
    "                for k in range(4):\n",
    "                    new_x = x + direction[k][0]\n",
    "                    new_y = y + direction[k][1]\n",
    "                    if new_x < 0 or new_x >= len(grid) or new_y < 0 or new_y >= len(grid[0]) or grid[new_x][new_y] == 0:\n",
    "                        continue\n",
    "                    if visited[new_x][new_y]:\n",
    "                        continue\n",
    "                    queue.append((new_x, new_y))\n",
    "                    visited[new_x][new_y] = True\n",
    "\n",
    "        for i in range(len(grid)):\n",
    "            if grid[i][0] == 1:\n",
    "                bfs(i, 0, grid, visited)\n",
    "            if grid[i][len(grid[0])-1] == 1:\n",
    "                bfs(i, len(grid[0])-1, grid, visited)\n",
    "        for j in range(len(grid[0])):\n",
    "            if grid[0][j] == 1:\n",
    "                bfs(0, j, grid, visited)\n",
    "            if grid[len(grid)-1][j] == 1:\n",
    "                bfs(len(grid)-1, j, grid, visited)\n",
    "        res = 0\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid[0])):\n",
    "                if grid[i][j] == 1 and not visited[i][j]:\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "#         nr = len(grid)\n",
    "#         nc = len(grid[0])\n",
    "\n",
    "#         sum_area = 0\n",
    "\n",
    "#         for r in range(nr):\n",
    "#             for c in range(nc):\n",
    "#                 if grid[r][c] == 1:\n",
    "#                     tmp_area = 1\n",
    "#                     grid[r][c] = 0\n",
    "#                     flag = True\n",
    "\n",
    "#                     neighbors = collections.deque([(r, c)])\n",
    "#                     while neighbors:\n",
    "#                         row, col = neighbors.popleft()\n",
    "\n",
    "#                         for x, y in [(row + 1, col), (row - 1, col), (row, col - 1), (row, col + 1)]:\n",
    "#                             if 0 <= x < nr and 0 <= y < nc and grid[x][y] == 1:\n",
    "#                                 grid[x][y] = 0\n",
    "#                                 tmp_area += 1\n",
    "#                                 neighbors.append((x, y))\n",
    "#                             elif x < 0 or y < 0 or x >= nr or y >= nc:\n",
    "#                                 flag = False\n",
    "                    \n",
    "#                     if flag:\n",
    "#                         sum_area += tmp_area\n",
    "#         return sum_area\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        vis = [[False] * n for _ in range(m)]\n",
    "        q = deque()\n",
    "        for i, row in enumerate(grid):\n",
    "            if row[0]:\n",
    "                vis[i][0] = True\n",
    "                q.append((i, 0))\n",
    "            if row[n - 1]:\n",
    "                vis[i][n - 1] = True\n",
    "                q.append((i, n - 1))\n",
    "        for j in range(1, n - 1):\n",
    "            if grid[0][j]:\n",
    "                vis[0][j] = True\n",
    "                q.append((0, j))\n",
    "            if grid[m - 1][j]:\n",
    "                vis[m - 1][j] = True\n",
    "                q.append((m - 1, j))\n",
    "        while q:\n",
    "            r, c = q.popleft()\n",
    "            for x, y in ((r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)):\n",
    "                if 0 <= x < m and 0 <= y < n and grid[x][y] and not vis[x][y]:\n",
    "                    vis[x][y] = True\n",
    "                    q.append((x, y))\n",
    "        return sum(grid[i][j] and not vis[i][j] for i in range(1, m - 1) for j in range(1, n - 1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import deque\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        if not grid:return(0)\n",
    "        vis = [[-1]*len(grid[0]) for i in range(len(grid))]\n",
    "        if len(grid[0])>1:\n",
    "            for i in range(len(grid[0])):\n",
    "                if grid[0][i] == 1: # 上\n",
    "                    bfs(0,i,grid,vis)\n",
    "            for i in range(len(grid[len(grid) - 1])): # 下\n",
    "                if grid[len(grid) - 1][i] == 1:\n",
    "                    bfs(len(grid) - 1, i, grid, vis)\n",
    "            for i in range(len(grid)): # 左\n",
    "                if grid[i][0] == 1:\n",
    "                    bfs(i, 0, grid, vis)\n",
    "            for i in range(len(grid)): # 右\n",
    "                if grid[i][len(grid[0]) -1]:\n",
    "                    bfs(i, len(grid[0]) - 1 , grid, vis)\n",
    "            count = 0\n",
    "            for i in range(len(grid)):\n",
    "                count += grid[i].count(1)\n",
    "            print(grid)\n",
    "            return(count)\n",
    "        else:\n",
    "            return(0)\n",
    "def bfs(i,j,grid,vis):\n",
    "    queue = deque()\n",
    "    queue.append([i,j])\n",
    "    vis[i][j] = 2\n",
    "    grid[i][j] = 2\n",
    "    _x = [1,-1,0,0]\n",
    "    _y = [0,0,1,-1]\n",
    "    while queue:\n",
    "        x,y = queue.popleft()\n",
    "        for z in range(4):\n",
    "            nx = x + _x[z]\n",
    "            ny = y + _y[z]\n",
    "            if nx < 0 or ny < 0 or nx >= len(grid) or ny >= len(grid[i]) :continue\n",
    "            if vis[nx][ny] != -1: continue\n",
    "            if grid[nx][ny] != 1:continue\n",
    "            vis[nx][ny] = 2\n",
    "            grid[nx][ny] = 2\n",
    "            queue.append([nx,ny])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class UnionFind:\n",
    "    def __init__(self, n: int):\n",
    "        self.parent = [x for x in range(n)]\n",
    "        self.size = [1 for _ in range(n)]\n",
    "        self.part = n\n",
    "    \n",
    "    def Find(self, x: int) -> int:\n",
    "        if self.parent[x] != x:\n",
    "            self.parent[x] = self.Find(self.parent[x])\n",
    "        return self.parent[x]\n",
    "    \n",
    "    def Union(self, x: int, y: int) -> bool:\n",
    "        root_x = self.Find(x)\n",
    "        root_y = self.Find(y)\n",
    "        if root_x == root_y:\n",
    "            return False\n",
    "        if self.size[root_x] > self.size[root_y]:\n",
    "            root_x, root_y = root_y, root_x\n",
    "        self.parent[root_x] = root_y\n",
    "        self.size[root_y] += self.size[root_x]\n",
    "        self.part -= 1\n",
    "        return True\n",
    "    \n",
    "    def connected(self, x: int, y: int) -> bool:\n",
    "        return self.Find(x) == self.Find(y)\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)]\n",
    "        Row = len(grid)\n",
    "        Col = len(grid[0])\n",
    "\n",
    "        UF = UnionFind(Row * Col + 1)\n",
    "        super_point = Row * Col         #超级原点\n",
    "\n",
    "        for r in range(Row):\n",
    "            for c in range(Col):\n",
    "                if grid[r][c] == 1:\n",
    "                    if r == 0 or r == Row - 1 or c == 0 or c == Col - 1:\n",
    "                        ID = r * Col + c\n",
    "                        UF.Union(ID, super_point)\n",
    "                    for di in range(4):\n",
    "                        dr = dirs[di][0]\n",
    "                        dc = dirs[di][1]\n",
    "                        nr = r + dr\n",
    "                        nc = c + dc\n",
    "                        if 0 <= nr < Row and 0 <= nc < Col and grid[nr][nc] == 1:\n",
    "                            ID = r * Col + c\n",
    "                            ID2 = nr * Col + nc\n",
    "                            UF.Union(ID, ID2)\n",
    "            \n",
    "        res = 0\n",
    "        for r in range(Row):\n",
    "            for c in range(Col):\n",
    "                if grid[r][c] == 1:\n",
    "                    ID = r * Col + c\n",
    "                    if UF.connected(ID, super_point) == False:\n",
    "                        res += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class UnionFind:\n",
    "    def __init__(self, grid: List[List[int]]):\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        self.parent = [0] * (m * n)\n",
    "        self.rank = [0] * (m * n)\n",
    "        self.onEdge = [False] * (m * n)\n",
    "        for i, row in enumerate(grid):\n",
    "            for j, v in enumerate(row):\n",
    "                if v:\n",
    "                    idx = i * n + j\n",
    "                    self.parent[idx] = idx\n",
    "                    if i == 0 or i == m - 1 or j == 0 or j == n - 1:\n",
    "                        self.onEdge[idx] = True\n",
    "\n",
    "    def find(self, x: int) -> int:\n",
    "        if self.parent[x] != x:\n",
    "            self.parent[x] = self.find(self.parent[x])\n",
    "        return self.parent[x]\n",
    "\n",
    "    def merge(self, x: int, y: int) -> None:\n",
    "        x, y = self.find(x), self.find(y)\n",
    "        if x == y:\n",
    "            return\n",
    "        if self.rank[x] > self.rank[y]:\n",
    "            self.parent[y] = x\n",
    "            self.onEdge[x] |= self.onEdge[y]\n",
    "        elif self.rank[x] < self.rank[y]:\n",
    "            self.parent[x] = y\n",
    "            self.onEdge[y] |= self.onEdge[x]\n",
    "        else:\n",
    "            self.parent[y] = x\n",
    "            self.onEdge[x] |= self.onEdge[y]\n",
    "            self.rank[x] += 1\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        uf = UnionFind(grid)\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        for i, row in enumerate(grid):\n",
    "            for j, v in enumerate(row):\n",
    "                if v:\n",
    "                    idx = i * n + j\n",
    "                    if j + 1 < n and grid[i][j + 1]:\n",
    "                        uf.merge(idx, idx + 1)\n",
    "                    if i + 1 < m and grid[i + 1][j]:\n",
    "                        uf.merge(idx, idx + n)\n",
    "        return sum(grid[i][j] and not uf.onEdge[uf.find(i * n + j)] for i in range(1, m - 1) for j in range(1, n - 1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        q = [(i, j) for i in range(m) for j in range(n) if (i == 0 or i == m - 1 or j == 0 or j == n - 1) and grid[i][j] == 1]\n",
    "        visited = set(q)\n",
    "        while q:\n",
    "            x, y = q.pop(0)\n",
    "            for nx, ny in [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]:\n",
    "                    if 0 <= nx < m and 0 <= ny < n and (nx, ny) not in visited and grid[nx][ny] == 1:\n",
    "                        visited.add((nx, ny))\n",
    "                        q.append((nx, ny))\n",
    "        res = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 1 and (i, j) not in visited:\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def numEnclaves(self, grid):\n",
    "        \"\"\"\n",
    "        :type grid: List[List[int]]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        lands=set()\n",
    "        nrow,ncol=len(grid),len(grid[0])\n",
    "        for ir in range(nrow):\n",
    "            for ic in range(ncol):\n",
    "                if grid[ir][ic]==1:\n",
    "                    lands.add((ir,ic))\n",
    "        if len(lands)==0:\n",
    "            return 0\n",
    "        dirs=[(0,1),(0,-1),(1,0),(-1,0),]\n",
    "        stack=[]\n",
    "\n",
    "        res=0\n",
    "        while lands:\n",
    "            tmp = lands.pop()\n",
    "            stack.append(tmp)\n",
    "            flag=True\n",
    "            tnum=1\n",
    "            while stack:\n",
    "                tmp=stack.pop()\n",
    "                for dir in dirs:\n",
    "                    x,y=tmp[0]+dir[0],tmp[1]+dir[1]\n",
    "                    if not (0<=x<nrow and 0<=y<ncol):\n",
    "                        flag=False\n",
    "                        continue\n",
    "                    if (x,y) in lands:\n",
    "                        lands.remove((x,y))\n",
    "                        stack.append((x,y))\n",
    "                        tnum+=1\n",
    "            if flag:\n",
    "                res+=tnum\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import deque\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        grid_node = {}\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        res = 0\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid[0])):\n",
    "                \n",
    "                if grid[i][j] == 1 and (i,j) not in grid_node.keys():\n",
    "                    tuan = 1\n",
    "                    q = deque()\n",
    "                    add = False\n",
    "                    q.append((i,j))\n",
    "                    grid_node[(i,j)] = True\n",
    "                    while q:\n",
    "                        x,y = q.popleft()\n",
    "                        \n",
    "                        if add == False and (x==0 or y==0 or x == m-1 or y ==n-1):\n",
    "                            add = True\n",
    "                        for (xi,yi) in [[-1,0],[1,0],[0,-1],[0,1]]:\n",
    "                            xx = x+xi\n",
    "                            yy = y+yi\n",
    "                            if xx<m and xx>=0 and yy<n and yy>=0 and (xx,yy) not in grid_node.keys() and grid[xx][yy]==1:\n",
    "                                tuan+=1\n",
    "                                grid_node[(xx,yy)] = True\n",
    "                                q.append((xx,yy))\n",
    "\n",
    "                    if not add:\n",
    "                        res+=tuan\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, mat: List[List[int]]) -> int:\n",
    "        m, n = len(mat), len(mat[0])\n",
    "        dxys = [(-1, 0), (+1, 0), (0, -1), (0, +1)]\n",
    "\n",
    "        x2p = [None for x in range(m*n+1)]\n",
    "        x2area = [None for x in range(m*n+1)]\n",
    "        x2p[m*n] = m*n\n",
    "        x2area[m*n] = 1\n",
    "        for x in range(m):\n",
    "            for y in range(n):\n",
    "                if mat[x][y] == 1:\n",
    "                    if x == 0 or x == m-1 or y == 0 or y == n-1:\n",
    "                        x2p[x * n + y] = m*n\n",
    "                        x2area[m*n] += 1\n",
    "                    else:\n",
    "                        x2p[x * n + y] = x * n + y\n",
    "                        x2area[x * n + y] = 1\n",
    "        def find(x: int) -> int:\n",
    "            if x2p[x] == x: return x2p[x]\n",
    "            else:\n",
    "                x2p[x] = find(x2p[x])\n",
    "                return x2p[x]\n",
    "        def same(x: int, y: int):\n",
    "            return find(x) == find(y)\n",
    "        def merge(x: int, y: int):\n",
    "            px, py = find(x), find(y)\n",
    "            if px == py: return\n",
    "            x2p[px] = py\n",
    "            x2area[py] += x2area[px]\n",
    "\n",
    "        for ux in range(m):\n",
    "            for uy in range(n):\n",
    "                if mat[ux][uy] != 1: continue\n",
    "                for dx, dy in dxys:\n",
    "                    vx, vy = ux + dx, uy + dy\n",
    "                    if not (vx >= 0 and vx < m and vy >= 0 and vy < n): continue\n",
    "                    if mat[vx][vy] != 1: continue\n",
    "                    merge(ux * n + uy, vx * n + vy)\n",
    "        ps = set()\n",
    "        for x in range(m):\n",
    "            for y in range(n):\n",
    "                if mat[x][y] == 1:\n",
    "                    ps.add(find(x * n + y))\n",
    "        ps.add(find(m*n))\n",
    "        ps.remove(find(m*n))\n",
    "        ans = 0\n",
    "        for p in ps:\n",
    "            ans += x2area[p]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        def find(x):\n",
    "            if fa[x]==x:\n",
    "                return x\n",
    "            fa[x]=find(fa[x])\n",
    "            return fa[x]\n",
    "        def union(x,y):\n",
    "            fa[find(x)]=find(y)\n",
    "            return\n",
    "        n=len(grid)\n",
    "        m=len(grid[0])\n",
    "        fa = list(range(n*m+1))\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if i==0 or j==0:\n",
    "                    union(i*m+j, n*m)\n",
    "                    continue\n",
    "                if grid[i][j]==1:\n",
    "                    if grid[i-1][j]==1:\n",
    "                        union(i*m+j, (i-1)*m+j)\n",
    "                    if grid[i][j-1]==1:\n",
    "                        union(i*m+j, i*m+j-1)\n",
    "                    if i==n-1 or j==m-1:\n",
    "                        union(i*m+j, n*m)\n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if grid[i][j]==1:\n",
    "                    if find(i*m+j)!=find(n*m):\n",
    "                        res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        ### solution\n",
    "        # dfs\n",
    "        # bfs\n",
    "        # dsu\n",
    "\n",
    "        ## dsu\n",
    "        def find(u):\n",
    "            if parent[u] != u:\n",
    "                parent[u] = find(parent[u])\n",
    "            return parent[u]\n",
    "        def union(u, v):\n",
    "            parent[find(u)] = parent[find(v)]\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        res = 0\n",
    "        parent = list(range(m * n + 1))\n",
    "        dummy = m * n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 1:\n",
    "                    if i == 0 or i == m - 1 or j == 0 or j == n - 1:\n",
    "                        union(i * n + j, dummy)\n",
    "                    for x, y in [(i + 1, j), (i, j + 1)]:\n",
    "                        if 0 <= x < m and 0 <= y < n and grid[x][y] == 1:\n",
    "                            union(i * n + j, x * n + y)\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 1:\n",
    "                    if find(dummy) != find(i * n + j):\n",
    "                        res += 1\n",
    "        return res\n",
    "\n",
    "        ## dfs\n",
    "        # connect all border\n",
    "        # count rest islands\n",
    "        def dfs(i, j):\n",
    "            nonlocal cnt\n",
    "            for x, y in [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]:\n",
    "                if 0 <= x < m and 0 <= y < n and grid[x][y] == 1 and not seen[x][y]:\n",
    "                    seen[x][y] = True\n",
    "                    cnt += 1\n",
    "                    dfs(x, y)\n",
    "            return\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        res, cnt = 0, 0\n",
    "        seen = [[False] * n for _ in range(m)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if i == 0 or i == m - 1 or j == 0 or j == n - 1:\n",
    "                    if grid[i][j] == 1 and not seen[i][j]:\n",
    "                        seen[i][j] = True\n",
    "                        dfs(i, j)\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 1 and not seen[i][j]:\n",
    "                    seen[i][j] = True\n",
    "                    cnt = 1\n",
    "                    dfs(i, j)\n",
    "                    res += cnt\n",
    "        return res\n",
    "\n",
    "        ## dsu older\n",
    "        def find(u):\n",
    "            if parent[u] != u:\n",
    "                parent[u] = find(parent[u])\n",
    "            return parent[u]\n",
    "        def union(u, v):\n",
    "            parent[find(u)] = parent[find(v)]\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        res, cnt = 0, 0\n",
    "        parent = list(range(m * n + 1))\n",
    "        dummy = m * n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 1:\n",
    "                    if i == 0 or i == m - 1 or j == 0 or j == n - 1:\n",
    "                        union(i * n + j, dummy)\n",
    "                    else:\n",
    "                        for x, y in [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]:\n",
    "                            if 0 <= x < m and 0 <= y < n and grid[x][y] == 1:\n",
    "                                union(i * n + j, x * n + y)\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 1:\n",
    "                    if find(dummy) != find(i * n + j):\n",
    "                        res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        m,n=len(grid),len(grid[0])\n",
    "        par=[i for i in range(m*n)]\n",
    "        ised=[False for i in range(m*n)]\n",
    "        size=[0 for i in range(m*n)]\n",
    "        def find(x):\n",
    "            if x==par[x]:\n",
    "                return x\n",
    "            return find(par[x])\n",
    "        def un(x,y):\n",
    "            rx,ry=find(x),find(y)\n",
    "            if rx==ry:\n",
    "                return False\n",
    "            par[ry]=rx\n",
    "            if ised[rx] or ised[ry]:\n",
    "                ised[rx]=True\n",
    "                ised[ry]=True\n",
    "      \n",
    "            return True\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if i==0 or j==0 or i==m-1 or j==n-1:\n",
    "                    if grid[i][j]==1:\n",
    "                        ised[i*n+j]=True\n",
    "            \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j]==1:\n",
    "                    if i+1<m and grid[i+1][j]==1:\n",
    "                        un(i*n+j,(i+1)*n+j)\n",
    "                    if j+1<n and grid[i][j+1]==1:\n",
    "                        un(i*n+j,i*n+j+1)\n",
    "        ans=0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                dx=n*i+j\n",
    "                if grid[i][j]==1 and ised[find(dx)]==False:\n",
    "                    ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        def find(idx):\n",
    "            if idx not in unionmap:\n",
    "                unionmap[idx] = idx\n",
    "            elif unionmap[idx] != idx:\n",
    "                unionmap[idx] = find(unionmap[idx])\n",
    "            return unionmap[idx]\n",
    "            \n",
    "        def union(i1, i2):\n",
    "            f1, f2 = find(i1), find(i2)\n",
    "            if f1 != f2:\n",
    "                unionmap[f2] = f1\n",
    "            \n",
    "        unionmap = {}\n",
    "        unionmap[-1] = -1\n",
    "        row, col = len(grid), len(grid[0])\n",
    "        for r, line in enumerate(grid):\n",
    "            for c, val in enumerate(line):\n",
    "                if val == 1:\n",
    "                    idx = r * col + c\n",
    "                    find(idx)\n",
    "                    if r == 0 or c == 0 or r == row - 1 or c == col - 1:\n",
    "                        union(-1, idx)\n",
    "                    if r + 1 <= row - 1 and grid[r + 1][c] == 1:\n",
    "                        union(idx, idx + col)\n",
    "                    if c + 1 <= col - 1 and grid[r][c + 1] == 1:\n",
    "                        union(idx, idx + 1)\n",
    "        return len([key for key in unionmap if find(key) != find(-1)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, board: List[List[int]]) -> int:\n",
    "        f = {}\n",
    "        def find(x):\n",
    "            f.setdefault(x, x)\n",
    "            if f[x] != x:\n",
    "                f[x] = find(f[x])\n",
    "            return f[x]\n",
    "        def union(x, y):\n",
    "            f[find(y)] = find(x)\n",
    "\n",
    "            \n",
    "            \n",
    "        if not board or not board[0]:\n",
    "            return\n",
    "        row = len(board)\n",
    "        col = len(board[0])\n",
    "        dummy = row * col\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                if board[i][j] == 1:\n",
    "                    if i == 0 or i == row - 1 or j == 0 or j == col - 1:\n",
    "                        union(i * col + j, dummy)\n",
    "                    else:\n",
    "                        for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1)]:\n",
    "                            if board[i + x][j + y] == 1:\n",
    "                                union(i * col + j, (i + x) * col + (j + y))\n",
    "        ans = 0\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                if board[i][j] and find(dummy) != find(i * col + j):\n",
    "                    ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# import collections\n",
    "import numpy as np\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        q = deque()\n",
    "        for i in range(0, len(grid)):\n",
    "            q.append((i, 0))\n",
    "\n",
    "        for i in range(0, len(grid)):\n",
    "            q.append((i, n-1))\n",
    "        \n",
    "        for i in range(0, n):\n",
    "            q.append((0, i))\n",
    "        for i in range(0, n):\n",
    "            q.append((m-1, i))\n",
    "\n",
    "        while q:\n",
    "            r, c = q.popleft()\n",
    "            if r < 0 or r >= m or c < 0 or c >= n or grid[r][c] == 0:\n",
    "                continue\n",
    "            grid[r][c] = 0\n",
    "            q.append((r-1, c))\n",
    "            q.append((r+1, c))\n",
    "            q.append((r, c-1))\n",
    "            q.append((r, c+1))\n",
    "    \n",
    "\n",
    "        return sum([sum(grid[i]) for i in range(len(grid))])\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "\n",
    "        visited=set()\n",
    "\n",
    "        self.flag=True\n",
    "        self.count=0\n",
    "\n",
    "        def dfs(i,j):\n",
    "            visited.add((i,j))\n",
    "            self.count+=1\n",
    "            if i-1<0: self.flag=False\n",
    "            else:\n",
    "                if grid[i-1][j]==1 and (i-1,j) not in visited: dfs(i-1,j)\n",
    "            if i+1==len(grid): self.flag=False\n",
    "            else:\n",
    "                if grid[i+1][j]==1 and (i+1,j) not in visited: dfs(i+1,j)\n",
    "            if j-1<0: self.flag=False\n",
    "            else:\n",
    "                if grid[i][j-1]==1 and (i,j-1) not in visited: dfs(i,j-1)\n",
    "            if j+1==len(grid[0]): self.flag=False\n",
    "            else:\n",
    "                if grid[i][j+1]==1 and (i,j+1) not in visited: dfs(i,j+1)\n",
    "\n",
    "        res=0\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid[0])):\n",
    "                if (i,j) not in visited and grid[i][j]==1:\n",
    "                    dfs(i,j)\n",
    "                    if self.flag: res+=self.count\n",
    "                    self.flag=True\n",
    "                    self.count=0\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        f = {}\n",
    "        count = [0] * (m*n+2)\n",
    "        def find(x):\n",
    "            if count[x] == 0 :\n",
    "                count[x] += 1\n",
    "            f.setdefault(x,x)\n",
    "            if f[x] != x:\n",
    "                f[x] = find(f[x])\n",
    "            return f[x]\n",
    "        def union(x,y):\n",
    "            f[find(y)] = find(x)\n",
    "        ans = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if i == 0 or i == m - 1 or j == 0 or j == n - 1:\n",
    "                    if grid[i][j] == 1:\n",
    "                        union(i*n+j,n*m)\n",
    "                if grid[i][j] == 1:\n",
    "                    for x,y in ([i+1,j],[i,j+1],[i-1,j],[i,j-1]):\n",
    "                        if 0 < x < m - 1 and 0 < y < n - 1 and grid[x][y] == 1:\n",
    "                            union(i*n+j,x*n+y)\n",
    "        \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 1 and find(i*n+j) != find(m*n):\n",
    "                    ans += 1\n",
    "        return (ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        import numpy as np\n",
    "        L3 = []\n",
    "        L4 = 0\n",
    "        def hanggengxin(grid):\n",
    "            L = []\n",
    "            for g1 in grid:\n",
    "                a1 = [2]\n",
    "                a2 = [2]\n",
    "                for g2 in g1:\n",
    "                    a1.append(g2)\n",
    "                a1.append(a2[0])  # 八个序列生成完毕 上下左右方向一共八个\n",
    "                # print(a1)\n",
    "                L.append(a1)\n",
    "            # print(L)\n",
    "            L2 = []\n",
    "            for g3 in L:\n",
    "                for g4 in range(len(g3)):\n",
    "                    if g4 + 1 < len(g3):  # 防止超范围报错\n",
    "                        if g3[g4] * g3[g4 + 1] == 2:\n",
    "                            g3[g4] = g3[g4 + 1] = 2\n",
    "                L2.append(g3)\n",
    "            # print(L2)  # L2是行更新完毕的状态。\n",
    "            if L == L2:\n",
    "                L3\n",
    "            return L2\n",
    "        def del2(grid):  # 这是去掉首尾的2 然后转置，实现一种转化功能。\n",
    "            grid1 = np.transpose(grid)\n",
    "            # print(grid1)\n",
    "            grid1 = grid1[1:-1]\n",
    "            # print(grid1)\n",
    "            return grid1\n",
    "        grid = hanggengxin(grid)  # 四行首尾加2，进行滑动计算\n",
    "        grid = del2(grid)  # 转置去2变成列。\n",
    "        grid = hanggengxin(grid)  # 四行首尾加2，进行滑动计算\n",
    "        grid = del2(grid)  # 转置去2变成行。\n",
    "        # def yanzheng(grid):\n",
    "        #     grid1 = grid\n",
    "        #     grid2 = hanggengxin(grid1)  # 四行首尾加2，进行滑动计算\n",
    "        #     grid3 = del2(grid2)  # 转置去2变成列。\n",
    "        #     grid4 = hanggengxin(grid3)  # 四行首尾加2，进行滑动计算\n",
    "        #     grid5 = del2(grid4)  # 转置去2变成行。\n",
    "        #     if grid5 == grid1:\n",
    "        #         print(\"结果已经确定\")\n",
    "        #     else:\n",
    "        #         grid = grid5\n",
    "        #         return grid\n",
    "        for i in range(len(grid)):\n",
    "            grid1 = grid\n",
    "            grid2 = hanggengxin(grid1)  # 四行首尾加2，进行滑动计算\n",
    "            grid3 = del2(grid2)  # 转置去2变成列。\n",
    "            grid4 = hanggengxin(grid3)  # 四行首尾加2，进行滑动计算\n",
    "            grid5 = del2(grid4)  # 转置去2变成行。\n",
    "            if (grid5 == grid1).all():\n",
    "                print(\"结果已经确定\")\n",
    "                print(grid5)\n",
    "                L3 = grid5\n",
    "                break\n",
    "            else:\n",
    "                grid = grid5\n",
    "        for a2 in L3:\n",
    "            for a3 in a2:\n",
    "                if a3 ==1:\n",
    "                    L4 +=1\n",
    "        return L4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    from enum import Enum\n",
    "\n",
    "    class Move(Enum):\n",
    "        up = (-1, 0)\n",
    "        down = (1, 0)\n",
    "        left = (0, -1)\n",
    "        right = (0, 1)\n",
    "\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        \"\"\"\n",
    "        DFS/BFS on all four edges with visited -> #non-enclaves (boundary islands)\n",
    "\n",
    "        #enclaves = #islands - #non-enclaves\n",
    "        \"\"\"\n",
    "        import numpy as np\n",
    "\n",
    "        self.grid = grid\n",
    "\n",
    "        self.visited = set()\n",
    "        self.n_boundary_islands = 0\n",
    "        m = len(self.grid)\n",
    "        n = len(self.grid[0])\n",
    "\n",
    "        # Search on four edges\n",
    "        # Top & Bottom\n",
    "        for x in range(n):\n",
    "            self._search_on_cell(0, x)\n",
    "            self._search_on_cell(m - 1, x)\n",
    "\n",
    "        # Left & Right\n",
    "        for y in range(m):\n",
    "            self._search_on_cell(y, 0)\n",
    "            self._search_on_cell(y, n - 1)\n",
    "\n",
    "        return int(np.sum(grid)) - self.n_boundary_islands\n",
    "\n",
    "    def _search_on_cell(self, y: int, x: int):\n",
    "        if (y, x) in self.visited:\n",
    "            return\n",
    "        if self.grid[y][x] == 0:\n",
    "            return\n",
    "\n",
    "        # self._dfs(y, x)\n",
    "        self._bfs(y, x)\n",
    "\n",
    "    def _dfs(self, y: int, x: int):\n",
    "        if (y, x) in self.visited:\n",
    "            return\n",
    "        if self._oob(y, x):\n",
    "            return\n",
    "        if self.grid[y][x] == 0:\n",
    "            return\n",
    "\n",
    "        self.visited.add((y, x))\n",
    "        self.n_boundary_islands += 1\n",
    "\n",
    "        for move in self.Move:\n",
    "            y_dir, x_dir = move.value\n",
    "            self._dfs(y + y_dir, x + x_dir)\n",
    "\n",
    "    def _bfs(self, y: int, x: int):\n",
    "        from collections import deque\n",
    "\n",
    "        queue = deque([(y, x)])\n",
    "        self.visited.add((y, x))\n",
    "        self.n_boundary_islands += 1\n",
    "\n",
    "        while queue:\n",
    "            cur_y, cur_x = queue.popleft()\n",
    "\n",
    "            for move in self.Move:\n",
    "                y_dir, x_dir = move.value\n",
    "                next_y, next_x = cur_y + y_dir, cur_x + x_dir\n",
    "\n",
    "                if (next_y, next_x) in self.visited:\n",
    "                    continue\n",
    "                if self._oob(next_y, next_x):\n",
    "                    continue\n",
    "                if self.grid[next_y][next_x] == 0:\n",
    "                    continue\n",
    "\n",
    "                queue.append((next_y, next_x))\n",
    "                self.n_boundary_islands += 1\n",
    "                self.visited.add((next_y, next_x))\n",
    "\n",
    "    def _oob(self, y: int, x: int) -> bool:\n",
    "        m = len(self.grid)\n",
    "        n = len(self.grid[0])\n",
    "        v_oob = (y < 0 or y >= m)\n",
    "        h_oob = (x < 0 or x >= n)\n",
    "\n",
    "        return v_oob or h_oob\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        m,n=len(grid),len(grid[0])\n",
    "        d=defaultdict(int)\n",
    "        def dfs(i,j,si,sj):\n",
    "            visited.add((i,j))\n",
    "            temp=[]\n",
    "            #nonlocal ans\n",
    "            res=1\n",
    "            for ni,nj in [[i+1,j],[i-1,j],[i,j+1],[i,j-1]]:\n",
    "                if not (0<=ni<m and 0<=nj<n):\n",
    "                    d[si,sj]=1\n",
    "                else:\n",
    "                    temp.append((ni,nj))\n",
    "            for ni,nj in temp:\n",
    "                if grid[ni][nj] and (ni,nj) not in visited:\n",
    "                    res+=dfs(ni,nj,si,sj)\n",
    "            return res\n",
    "        visited=set()\n",
    "        ans=0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] and (i,j) not in visited:\n",
    "                    res=dfs(i,j,i,j)\n",
    "                    if not d[i,j]:\n",
    "                        ans+=res\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        # for i in grid:\n",
    "        #     print(i)\n",
    "        def find(i):\n",
    "            if parent[i]!=i:\n",
    "                parent[i]=find(parent[i])\n",
    "            return parent[i]\n",
    "        def union(i,j):\n",
    "            parent[find(i)]=find(j)\n",
    "\n",
    "        m,n = len(grid),len(grid[0])\n",
    "        parent = {(-1,-1):(-1,-1)}\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j]==1:\n",
    "                    if i==0 or i==m-1 or j==0 or j==n-1:\n",
    "                        parent[(i,j)] = (-1,-1)\n",
    "                    else:\n",
    "                        parent[(i,j)]=(i,j)\n",
    "        # print(parent)\n",
    "        # visited = [[0]*m for _ in range(n)]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j]==1:\n",
    "                    # visited[i][j]=1\n",
    "                    for x,y in [(i-1,j),(i+1,j),(i,j-1),(i,j+1)]:\n",
    "                        if 0<=x<m and 0<=y<n and grid[x][y]==1:\n",
    "                            union((i,j),(x,y))\n",
    "                            # if find((i,j))!=find((x,y)):\n",
    "                            #     if x==0 or x==n-1 or y==0 or y==n-1:\n",
    "                            #         union((i,j),(x,y))\n",
    "                            #     else:\n",
    "                            #         union((x,y),(i,j))\n",
    "        # print(parent)\n",
    "        count = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j]==1 and find((i,j))!=find((-1,-1)):\n",
    "                    print((i,j))\n",
    "                    count+=1\n",
    "        # print(parent)\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "class Solution:\n",
    "    @lru_cache(None)\n",
    "    def next(self,rowid,colid):\n",
    "        res = []\n",
    "        for i, j in ((rowid - 1, colid), (rowid, colid - 1), (rowid + 1, colid), (rowid, colid + 1)):\n",
    "            if 0 <= i < self.height and 0 <= j < self.width:\n",
    "                res.append([i, j])\n",
    "        return res\n",
    "\n",
    "    def dfs(self,r,c):\n",
    "        self.grid[r][c]=0\n",
    "        for nextr,nextc in self.next(r,c):\n",
    "            if self.grid[nextr][nextc]==1:\n",
    "                self.grid[nextr][nextc] = 0\n",
    "                self.dfs(nextr,nextc)\n",
    "\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        self.grid = grid\n",
    "        self.height,self.width = len(grid),len(grid[0])\n",
    "        # print(np.array(self.grid))\n",
    "        for r in [0,self.height-1]:\n",
    "            for c in range(self.width):\n",
    "                if self.grid[r][c]:\n",
    "                    self.dfs(r,c)\n",
    "\n",
    "        for c in [0,self.width-1]:\n",
    "            for r in range(self.height):\n",
    "                if self.grid[r][c]:\n",
    "                    self.dfs(r,c)\n",
    "        # print(np.array(self.grid))\n",
    "        return int(np.array(self.grid).sum())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy as np\n",
    "class Solution:\n",
    "    @lru_cache(None)\n",
    "    def next(self,rowid,colid):\n",
    "        res = []\n",
    "        for i, j in ((rowid - 1, colid), (rowid, colid - 1), (rowid + 1, colid), (rowid, colid + 1)):\n",
    "            if 0 <= i < self.height and 0 <= j < self.width:\n",
    "                res.append([i, j])\n",
    "        return res\n",
    "\n",
    "    def dfs(self,r,c):\n",
    "        self.grid[r][c]=0\n",
    "        for nextr,nextc in self.next(r,c):\n",
    "            if self.grid[nextr][nextc]==1:\n",
    "                self.grid[nextr][nextc] = 0\n",
    "                self.dfs(nextr,nextc)\n",
    "\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        self.grid = grid\n",
    "        self.height,self.width = len(grid),len(grid[0])\n",
    "        # print(np.array(self.grid))\n",
    "        for r in [0,self.height-1]:\n",
    "            for c in range(self.width):\n",
    "                if self.grid[r][c]:\n",
    "                    self.dfs(r,c)\n",
    "\n",
    "        for c in [0,self.width-1]:\n",
    "            for r in range(self.height):\n",
    "                if self.grid[r][c]:\n",
    "                    self.dfs(r,c)\n",
    "        # print(np.array(self.grid))\n",
    "        return int(np.array(self.grid).sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "#https://github.com/azl397985856/leetcode/blob/master/problems/1020.number-of-enclaves.md\n",
    "class Solution:\n",
    "    def numEnclaves(self, A: List[List[int]]) -> int:\n",
    "        ###DFS\n",
    "        ###暴力法\n",
    "        cnt=0\n",
    "        m=len(A)\n",
    "        n=len(A[0])\n",
    "        visited=set()\n",
    "        \n",
    "        def dfs(i,j):\n",
    "            if i<0 or i>=m or j<0 or j>=n or (i,j) in visited:\n",
    "                return\n",
    "            visited.add((i,j))\n",
    "            if A[i][j]==1:\n",
    "                self.temp+=1\n",
    "            else:\n",
    "                return\n",
    "            if i==0 or i==m-1 or j==0 or j==n-1:\n",
    "                self.meetEdge=True\n",
    "            dfs(i+1,j)\n",
    "            dfs(i-1,j)\n",
    "            dfs(i,j+1)\n",
    "            dfs(i,j-1)\n",
    "\n",
    "        self.meetEdge=False\n",
    "        self.temp=0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                dfs(i,j)\n",
    "                if not self.meetEdge:\n",
    "                    cnt+=self.temp\n",
    "                self.meetEdge=False\n",
    "                self.temp=0\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        vis = set()\n",
    "\n",
    "        nb = [[1, 0], [-1, 0], [0, 1], [0, -1]]\n",
    "        \n",
    "        def dfs(i, j):\n",
    "            if (i, j) in vis:\n",
    "                return 0\n",
    "            vis.add((i, j))\n",
    "            grid[i][j] = 0\n",
    "            res = 1\n",
    "            for [dx, dy] in nb:\n",
    "                cx, cy = i + dx, j + dy\n",
    "                if cx < 0 or cy < 0 or cx > m - 1 or cy > n - 1:\n",
    "                    continue\n",
    "                if (cx, cy) in vis:\n",
    "                    continue\n",
    "                if grid[cx][cy] == 0:\n",
    "                    vis.add((cx, cy))\n",
    "                    continue\n",
    "\n",
    "                res += dfs(cx, cy)\n",
    "            return res\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 0:\n",
    "                    continue\n",
    "                if i == 0 or j == 0 or i == m - 1 or j == n - 1:\n",
    "                    res = dfs(i, j)\n",
    "                    # print(i, j, res)\n",
    "                    \n",
    "        # print(grid)\n",
    "\n",
    "        res = 0\n",
    "        for i in range(1, m - 1):\n",
    "            for j in range(1, n - 1):\n",
    "                if grid[i][j] == 0:\n",
    "                    continue\n",
    "                res += dfs(i, j)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    temp=0\n",
    "    meetEdge = False\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        cnt=0\n",
    "        m = len(grid)\n",
    "        n=len(grid[0])\n",
    "        visited = set()\n",
    "        def dfs(i,j):\n",
    "            if i<0 or i>=m or j<0 or j>=n or (i,j) in visited:\n",
    "                return\n",
    "            visited.add((i,j))\n",
    "            if grid[i][j]==1:\n",
    "                self.temp+=1\n",
    "            else:\n",
    "                return\n",
    "            if i==0 or i==m-1 or j==0 or j==n-1:\n",
    "                self.meetEdge = True\n",
    "            dfs(i+1,j)\n",
    "            dfs(i-1,j)\n",
    "            dfs(i,j+1)\n",
    "            dfs(i,j-1)\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                dfs(i,j)\n",
    "                if not self.meetEdge:\n",
    "                    cnt+=self.temp\n",
    "                self.meetEdge=False\n",
    "                self.temp=0\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "import numpy\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "\n",
    "        def dfs(x, y):  \n",
    "            grid[x][y] = 0\n",
    "            for k, v in [[x-1, y], [x+1, y], [x, y-1], [x, y+1]]:\n",
    "                if 0 <= k < m and 0 <= v < n and grid[k][v]:\n",
    "                    dfs(k,v)\n",
    "                    \n",
    "        for i in range(n):\n",
    "            if grid[0][i]:\n",
    "                dfs(0, i)\n",
    "            if grid[m-1][i]:\n",
    "                dfs(m-1, i)\n",
    "        for j in range(1, m-1):\n",
    "            if grid[j][0]:\n",
    "                dfs(j, 0)\n",
    "            if grid[j][n-1]:\n",
    "                dfs(j, n-1)\n",
    "    \n",
    "        return sum(map(sum,grid))  \n",
    "s = Solution()\n",
    "an = s.numEnclaves([[0,0,0,1,1,1,0,1,1,0,1,0,1,0,0,0,1,1,0,0,0,1,0,1,1,1,1,1,0,0,1,1,0,0,1,1,1,1,0,0,0,1,1,1,1,0,0,0,0,1,1,1,0,0,1,1,0,0,0,1,0,1,1,0,1,0,1,0,1,0,1,1,0,0,1,0,1,0,0,0,1,0,0,1,0,0,1,0,1,1,0,0,1,1,1,0,1,0,0,1,0,1,1,0,0,0,1,0,1,1,0,1,1,1,1,0,0,1,1,0,1,0,1,1,1,0,1,0,1,1,0,1,0,1,0,1,1,0,0,1,1,1,0,0,1,0,1,1,0,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,1,0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,1,1,0,0,0,0,1],[0,1,0,1,1,1,0,0,1,0,0,1,1,0,1,1,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,1,0,1,1,0,0,0,1,0,0,1,1,1,1,1,1,0,0,1,1,0,1,0,0,0,1,0,0,1,1,1,1,0,1,1,0,1,0,1,1,1,0,0,0,0,0,1,1,0,1,1,0,0,1,1,0,0,1,1,0,0,1,0,0,0,1,0,0,0,1,1,1,1,1,0,0,1,0,0,0,0,0,0,1,0,1,1,1,1,1,0,1,1,1,0,1,1,1,0,1,0,1,0,1,1,1,1,1,0,1,0,0,0,1,1,0,0,1,1,1,1,1,0,0,0,0,1,1,0,1,1,1,1,1,0,1,1,0,0,1,1,1,0,0,1,0,0,0,0,1,0,1,0,0,1,0,0,0,1],[1,1,1,1,1,1,1,0,0,0,1,0,1,0,1,1,1,0,1,1,0,1,1,0,0,1,1,0,1,0,1,1,1,0,0,1,0,1,0,0,1,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,1,0,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0,1,0,0,0,0,1,0,0,0,1,1,0,0,1],[1,1,0,0,1,1,0,0,1,0,1,1,0,1,0,1,1,0,0,1,0,0,0,1,1,0,0,0,0,0,1,1,0,1,1,1,1,0,1,1,0,0,0,0,1,0,1,0,1,0,1,1,1,0,1,1,0,1,1,0,1,0,1,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,1,0,0,1,0,0,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,1,1,1,0,0,0,1,0,1,0,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,1,1,1,1,0,0,0,1,0,1,0,1,1,1,1,1,1,0,0,0,0,1,0,1,0,1,1,0,1,1,1,0,0,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,0,0,1,0,0,1,0,0,0,1]])\n",
    "print(an)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, A) -> int:\n",
    "        \n",
    "        def find(x):\n",
    "            if p[x] != x:\n",
    "                p[x] = find(p[x])\n",
    "            return p[x]\n",
    "        \n",
    "        border = set()\n",
    "        row, col = len(A), len(A[0])\n",
    "        p = { (i,j):(i,j) for i in range(row) for j in range(col) }\n",
    "        \n",
    "        for i in range(row):\n",
    "            border.add((i,0))\n",
    "            border.add((i,col-1))\n",
    "            \n",
    "        for i in range(col):\n",
    "            border.add((0,i))\n",
    "            border.add((row-1,i))\n",
    "        \n",
    "        def union(x,y):\n",
    "            px, py = find(x), find(y)\n",
    "            if px in border:\n",
    "                p[py] = px\n",
    "            else:\n",
    "                p[px] = py\n",
    "        \n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                if A[i][j] == 1:\n",
    "                    for x,y in ((i+1,j),(i,j+1)):\n",
    "                        if x < row and y < col and A[x][y] == 1:\n",
    "                            union((x,y),(i,j))\n",
    "        \n",
    "        res = 0\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                if A[i][j] == 1 and find((i,j)) not in border:\n",
    "                    res += 1\n",
    "        return res\n",
    "                \n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        n=len(grid)\n",
    "        m=len(grid[0])\n",
    "        def neighbor(x,y):\n",
    "            result=[]\n",
    "            if x>0:\n",
    "                result.append([x-1,y])\n",
    "            if x<n-1:\n",
    "                result.append([x+1,y])\n",
    "            if y>0:\n",
    "                result.append([x,y-1])\n",
    "            if y<m-1:\n",
    "                result.append([x,y+1])\n",
    "            return result \n",
    "        temp=collections.defaultdict(int)\n",
    "        on_border=collections.defaultdict(int)\n",
    "        def border(x,y):\n",
    "            return x==0 or x==n-1 or y==0 or y==m-1\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                temp[(i,j)]=(i,j)\n",
    "        def find(x):\n",
    "            if temp[x]==x:\n",
    "                return x\n",
    "            now=find(temp[x])\n",
    "            temp[x]=now \n",
    "            return now \n",
    "        def connect(x,y):\n",
    "            y_fa=find(y)\n",
    "            x_fa=find(x)\n",
    "            temp[y_fa]=find(x)\n",
    "            if on_border[y_fa]==1:\n",
    "                on_border[x_fa]=1\n",
    "            return \n",
    "        visit=[[0]*m for _ in range(n)]\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if grid[i][j]==1 and border(i,j):\n",
    "                    on_border[(i,j)]=1\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if grid[i][j]==1:\n",
    "                    for v in neighbor(i,j):\n",
    "                        if grid[v[0]][v[1]]==0:\n",
    "                            continue\n",
    "                        v=tuple(v)\n",
    "                        connect(v,(i,j))\n",
    "        result=0\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if grid[i][j]==1 and on_border[find((i,j))]==0:\n",
    "                    result+=1\n",
    "        #print(find((0,2)))\n",
    "        return result\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        m=len(grid)\n",
    "        n=len(grid[0])\n",
    "        parent={(i,j):(i,j) for i,j in product(range(m),range(n))}\n",
    "        cnt={(i,j):1 for i,j in product(range(m),range(n))}\n",
    "        def find(x):\n",
    "            if x!=parent[x]:\n",
    "                parent[x]=find(parent[x])\n",
    "            return parent[x]\n",
    "        def union(a,b):\n",
    "            x,y=find(a),find(b)\n",
    "            if x!=y:\n",
    "                if y[0]==0 or y[0]==m-1 or y[1]==0 or y[1]==n-1:\n",
    "                    parent[x]=y\n",
    "                    cnt[y]+=cnt[x]\n",
    "                else:\n",
    "                    parent[y]=x\n",
    "                    cnt[x]+=cnt[y]\n",
    "        def sz(x):\n",
    "            return cnt[find(x)]\n",
    "        for i,j in product(range(m),range(n)):\n",
    "            if grid[i][j]==1:\n",
    "                if i>0 and grid[i-1][j]==1:\n",
    "                    union((i,j),(i-1,j))\n",
    "                if j>0 and grid[i][j-1]==1:\n",
    "                    union((i,j),(i,j-1))\n",
    "        hashset=set()\n",
    "        summ=0\n",
    "        part=0\n",
    "        for i,j in product(range(m),range(n)):\n",
    "            if grid[i][j]==1 and find((i,j)) not in hashset:\n",
    "                p,q=find((i,j))\n",
    "                if p==0 or p==m-1 or q==0 or q==n-1:\n",
    "                    summ+=sz((i,j))\n",
    "                    part+=sz((i,j))\n",
    "                else:\n",
    "                    summ+=sz((i,j))\n",
    "                hashset.add(find((i,j)))\n",
    "\n",
    "        return summ-part"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        res = 0\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        def dfs(grid,i,j):\n",
    "            if not 0 <= i < len(grid) or not 0 <= j < len(grid[0]) or grid[i][j] == \"0\": return\n",
    "            if grid[i][j] == 0:\n",
    "                return\n",
    "            grid[i][j] = 0\n",
    "            dfs(grid, i+1, j)\n",
    "            dfs(grid, i, j+1)\n",
    "            dfs(grid, i-1, j)\n",
    "            dfs(grid, i, j-1)\n",
    "        for i in range(m):\n",
    "            dfs(grid, i, 0)\n",
    "            dfs(grid, i, n-1)\n",
    "        for j in range(n):\n",
    "            dfs(grid, 0, j)\n",
    "            dfs(grid, m-1, j)\n",
    "        \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 1:\n",
    "                    res += 1\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "\n",
    "        # 查找包含grid[i][j]的完整岛屿，并将其淹没\n",
    "        def backtrack(grid, i, j):\n",
    "\n",
    "            if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]):\n",
    "                return\n",
    "\n",
    "            if grid[i][j] == 0:\n",
    "                return\n",
    "            \n",
    "            grid[i][j] = 0\n",
    "\n",
    "            backtrack(grid, i+1, j)\n",
    "            backtrack(grid, i, j+1)\n",
    "            backtrack(grid, i-1, j)\n",
    "            backtrack(grid, i, j-1)\n",
    "\n",
    "        # 先把链接四边的岛屿剔除\n",
    "        for i in range(len(grid)):\n",
    "            backtrack(grid, i, 0)\n",
    "            backtrack(grid, i, len(grid[0])-1)\n",
    "        for j in range(len(grid[0])):\n",
    "            backtrack(grid, 0, j)\n",
    "            backtrack(grid, len(grid)-1, j)\n",
    "\n",
    "        # 查找所有剩下的岛屿\n",
    "        count = 0\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid[0])):\n",
    "                if grid[i][j] == 1:\n",
    "                    count += 1\n",
    "        \n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        self.grid = grid\n",
    "        self.m = m = len(grid)\n",
    "        self.n = n = len(grid[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if i == 0 or i == m - 1 or j == 0 or j == n - 1:\n",
    "                    if grid[i][j] == 1:\n",
    "                        self.dfs(i, j)\n",
    "        return sum(sum(row) for row in self.grid)\n",
    "\n",
    "    def dfs(self, i, j):\n",
    "        if i < 0 or i >= self.m or j < 0 or j >= self.n or self.grid[i][j] == 0:\n",
    "            return\n",
    "        self.grid[i][j] = 0\n",
    "        self.dfs(i + 1, j)\n",
    "        self.dfs(i - 1, j)\n",
    "        self.dfs(i, j + 1)\n",
    "        self.dfs(i, j - 1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        row, col = len(grid), len(grid[0])\n",
    "        ans = 0\n",
    "\n",
    "        #### 1. DFS\n",
    "        def process(x, y):\n",
    "            if x < 0 or x>= row or y <0 or y>=col: return 0\n",
    "            if grid[x][y] != 1: return 0\n",
    "            grid[x][y] = 2\n",
    "            return 1 + process(x-1, y) + process(x+1, y) + process(x, y+1) + process(x, y-1)\n",
    "\n",
    "        \n",
    "        # 先找到所有的1的数量，连接在边缘的1的数量\n",
    "        # 注意，需要1在DFS中会被修改成2，也要加上这部分\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                if grid[i][j] in [1, 2]:\n",
    "                    ans += 1\n",
    "                    if i == 0 or i==row-1 or j== 0 or j==col-1:\n",
    "                        ans -= process(i, j)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        if not grid:\n",
    "            return 0\n",
    "        rows = len(grid)\n",
    "        cols = len(grid[0])\n",
    "        visited = [[False] * cols for _ in range(rows)]\n",
    "        for row in range(rows):\n",
    "            if grid[row][0] == 1 and not visited[row][0]:\n",
    "                self.dfs(row, 0, grid , visited)\n",
    "            if grid[row][cols - 1] == 1 and not visited[row][cols - 1]:\n",
    "                self.dfs(row , cols - 1, grid, visited)\n",
    "        for col in range(cols):\n",
    "            if grid[0][col] == 1 and not visited[0][col]:\n",
    "                self.dfs(0, col, grid, visited)\n",
    "            if grid[rows-1][col] == 1 and not visited[rows-1][col]:\n",
    "                self.dfs(rows-1, col, grid, visited)\n",
    "        count = 0\n",
    "        for row in range(rows):\n",
    "            for col in range(cols):\n",
    "                if grid[row][col] == 1 and not visited[row][col]:\n",
    "                    count += 1\n",
    "        return count\n",
    "    def dfs(self, row, col, grid, visited):\n",
    "        rows = len(grid)\n",
    "        cols = len(grid[0])\n",
    "        if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == 0 or visited[row][col]:\n",
    "            return\n",
    "        visited[row][col] = True\n",
    "        self.dfs(row+1, col, grid, visited)\n",
    "        self.dfs(row-1, col, grid, visited)\n",
    "        self.dfs(row, col+1, grid, visited)\n",
    "        self.dfs(row, col-1, grid, visited)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        m,n=len(grid),len(grid[0])\n",
    "        a=[[0 for j in range(n)]for i in range (m)]\n",
    "        def dfs(x,y,n,m):\n",
    "            if  x<0 or x>=n or y<0 or y>=m or a[y][x]==1 or grid[y][x]==0:\n",
    "                return\n",
    "            a[y][x]=1\n",
    "            dfs(x+1,y,n,m)\n",
    "            dfs(x,y+1,n,m)\n",
    "            dfs(x-1,y,n,m)\n",
    "            dfs(x,y-1,n,m)\n",
    "            return\n",
    "        for i in range(n):\n",
    "            if grid[0][i]!=0 and a[0][i]!=1:\n",
    "                dfs(i,0,n,m)\n",
    "        for i in range(n):\n",
    "            if grid[m-1][i]!=0 and a[m-1][i]!=1:\n",
    "                dfs(i,m-1,n,m)\n",
    "        for i in range(m):\n",
    "            if grid[i][0]!=0 and a[i][0]!=1:\n",
    "                dfs(0,i,n,m)\n",
    "        for i in range(m):\n",
    "            if grid[i][n-1]!=0 and a[i][n-1]!=1:\n",
    "                dfs(n-1,i,n,m)\n",
    "        cnt=0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if a[i][j]==0 and grid[i][j]==1:\n",
    "                    cnt+=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        res = 0\n",
    "        rows, cols = len(grid), len(grid[0])\n",
    "\n",
    "        def func(x, y):\n",
    "            # if not self.valid:\n",
    "            #     return\n",
    "            try:\n",
    "                value = grid[x][y]\n",
    "            except IndexError:\n",
    "                return \n",
    "            else:\n",
    "                if value != 1:\n",
    "                    return\n",
    "                if x == 0 or x == rows-1 or y == 0 or y== cols-1:\n",
    "                    self.valid = False\n",
    "                    return\n",
    "                \n",
    "                grid[x][y] = -1\n",
    "                self.cur += 1\n",
    "                func(x-1, y)\n",
    "                func(x+1, y)\n",
    "                func(x, y+1)\n",
    "                func(x, y-1)\n",
    "        \n",
    "        for i in range(rows):\n",
    "            for j in range(cols):\n",
    "                if grid[i][j] == 1:\n",
    "                    self.cur = 0\n",
    "                    self.valid = True\n",
    "                    func(i, j)\n",
    "                    if self.valid:\n",
    "                        res += self.cur\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "\n",
    "#     def __init__(self):\n",
    "#         self.position = [(-1, 0), (0, 1),(1, 0), (0, -1)]\n",
    "\n",
    "#     def dfs(self, grid, visited, x, y):\n",
    "#         for dx, dy in self.position:\n",
    "#             new_x = x + dx\n",
    "#             new_y = y + dy\n",
    "#             if new_x < 0 or new_x >= len(grid) or new_y <0 or new_y>=len(grid[0]):\n",
    "#                 continue\n",
    "#             if visited[new_x][new_y] or grid[new_x][new_y] == 0:\n",
    "#                 continue\n",
    "#             visited[new_x][new_y] = True\n",
    "#             self.dfs(grid, visited, new_x, new_y)\n",
    "\n",
    "#     def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "#         m = len(grid)\n",
    "#         n = len(grid[0])\n",
    "#         visited = [[False]*n for _ in range(m)]\n",
    "#         res = 0\n",
    "#         for i in range(m):\n",
    "#             if grid[i][0] == 1:\n",
    "#                 visited[i][0] = True\n",
    "#                 self.dfs(grid, visited, i, 0)\n",
    "#             if grid[i][n-1] == 1:\n",
    "#                 visited[i][n-1] = True\n",
    "#                 self.dfs(grid, visited, i, n-1)\n",
    "        \n",
    "#         for j in range(1,n-1):\n",
    "#             if grid[0][j] == 1:\n",
    "#                 visited[0][j] = True\n",
    "#                 self.dfs(grid, visited, 0, j)\n",
    "#             if grid[m-1][j] ==1:\n",
    "#                 visited[m-1][j] = True\n",
    "#                 self.dfs(grid, visited, m-1, j)\n",
    "#         for i in range(m):\n",
    "#             for j in range(n):\n",
    "#                 if grid[i][j] == 1 and not visited[i][j]:\n",
    "#                     res += 1\n",
    "#         return res\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def __init__(self):\n",
    "        self.position = [[-1, 0], [0, 1], [1, 0], [0, -1]]\t# 四个方向\n",
    "\n",
    "    # 深度优先遍历，把可以通向边缘部分的 1 全部标记成 true\n",
    "    def dfs(self, grid: List[List[int]], row: int, col: int, visited: List[List[bool]]) -> None:\n",
    "        for current in self.position:\n",
    "            newRow, newCol = row + current[0], col + current[1]\n",
    "            # 索引下标越界\n",
    "            if newRow < 0 or newRow >= len(grid) or newCol < 0 or newCol >= len(grid[0]): \n",
    "                continue\n",
    "            # 当前位置值不是 1 或者已经被访问过了\n",
    "            if grid[newRow][newCol] == 0 or visited[newRow][newCol]: continue\n",
    "            visited[newRow][newCol] = True\n",
    "            self.dfs(grid, newRow, newCol, visited)\n",
    "\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        rowSize, colSize, ans = len(grid), len(grid[0]), 0\n",
    "        # 标记数组记录每个值为 1 的位置是否可以到达边界，可以为 True，反之为 False\n",
    "        visited = [[False for _ in range(colSize)] for _ in range(rowSize)]\n",
    "        # 搜索左边界和右边界，对值为 1 的位置进行深度优先遍历\n",
    "        for row in range(rowSize):\n",
    "            if grid[row][0] == 1:\n",
    "                visited[row][0] = True\n",
    "                self.dfs(grid, row, 0, visited)\n",
    "            if grid[row][colSize - 1] == 1:\n",
    "                visited[row][colSize - 1] = True\n",
    "                self.dfs(grid, row, colSize - 1, visited)\n",
    "        # 搜索上边界和下边界，对值为 1 的位置进行深度优先遍历，但是四个角不需要，因为上面遍历过了\n",
    "        for col in range(1, colSize - 1):\n",
    "            if grid[0][col] == 1:\n",
    "                visited[0][col] = True\n",
    "                self.dfs(grid, 0, col, visited)\n",
    "            if grid[rowSize - 1][col] == 1:\n",
    "                visited[rowSize - 1][col] = True\n",
    "                self.dfs(grid, rowSize - 1, col, visited)\n",
    "        # 找出矩阵中值为 1 但是没有被标记过的位置，记录答案\n",
    "        for row in range(rowSize):\n",
    "            for col in range(colSize):\n",
    "                if grid[row][col] == 1 and not visited[row][col]:\n",
    "                    ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        width, height, ans = len(grid[0]), len(grid), 0\n",
    "        visited = [[False]*width for _ in range(height)]\n",
    "        pos = [[-1,0], [1,0], [0,1], [0,-1]]\n",
    "        for j in range(width):\n",
    "            if grid[0][j] == 1:\n",
    "                visited[0][j] = True\n",
    "                self.dfs(grid, visited, 0, j, pos)\n",
    "            if grid[height-1][j] == 1:\n",
    "                visited[height-1][j] = True\n",
    "                self.dfs(grid, visited, height-1, j, pos)\n",
    "        for i in range(height):\n",
    "            if grid[i][0] == 1:\n",
    "                visited[i][0] = True\n",
    "                self.dfs(grid, visited, i, 0, pos)\n",
    "            if grid[i][width-1] == 1:\n",
    "                visited[i][width-1] = True\n",
    "                self.dfs(grid, visited, i, width-1, pos)\n",
    "        for i in range(height):\n",
    "            for j in range(width):\n",
    "                if grid[i][j] == 1 and not visited[i][j]: ans += 1\n",
    "        return ans\n",
    "\n",
    "    def dfs(self, grid, visited, raw, col, pos):\n",
    "        for x,y in pos:\n",
    "            next_x = raw + x\n",
    "            next_y = col + y\n",
    "            if 0 <= next_x <len(grid) and 0 <= next_y < len(grid[0]) and not visited[next_x][next_y] and grid[next_x][next_y] == 1:\n",
    "                visited[next_x][next_y] = True\n",
    "                self.dfs(grid, visited, next_x, next_y, pos)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        visited = [[False]*n for _ in range(m)]\n",
    "        dirs = [(-1,0), (1,0), (0,1), (0,-1)]\n",
    "        \n",
    "        def dfs(r, c):\n",
    "            if r < 0 or r >=m or c<0 or c>=n or grid[r][c] == 0 or visited[r][c]:\n",
    "                return\n",
    "            visited[r][c] = True\n",
    "            for x, y in dirs:\n",
    "                dfs(r+x, c+y)\n",
    "        \n",
    "        for i in range(m):\n",
    "            dfs(i,0)\n",
    "            dfs(i, n-1)\n",
    "        for j in range(n):\n",
    "            dfs(0,j)\n",
    "            dfs(m-1,j)\n",
    "        return sum(grid[i][j] and not visited[i][j] for i in range(m) for j in range(n))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        res=0\n",
    "        m,n=len(grid),len(grid[0])\n",
    "\n",
    "        visit=[[False for _ in range(n)] for _ in range(m)]\n",
    "        path=[[0,1],[0,-1],[1,0],[-1,0]]\n",
    "\n",
    "        def dfs(x,y,res_this):\n",
    "            # print(x,y)\n",
    "            isboard=False\n",
    "            if x==0 or y==0 or x==m-1 or y==n-1:\n",
    "                isboard=True\n",
    "            \n",
    "            for p in path:\n",
    "                newx=x+p[0]\n",
    "                newy=y+p[1]\n",
    "\n",
    "                if newx<0 or newy<0 or newx>m-1 or newy>n-1:\n",
    "                    continue\n",
    "\n",
    "                if not visit[newx][newy] and grid[newx][newy]==1:\n",
    "                    res_this+=1\n",
    "                    visit[newx][newy]=True\n",
    "                    isboard_this,res_this=dfs(newx,newy,res_this)\n",
    "                    if isboard_this==True:\n",
    "                        isboard=True\n",
    "\n",
    "            return isboard,res_this\n",
    "\n",
    "\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if not visit[i][j] and grid[i][j]==1:\n",
    "                    res_this=1\n",
    "                    visit[i][j]=True\n",
    "                    isboard,res_this=dfs(i,j,res_this)\n",
    "                    if not isboard:\n",
    "                        res+=res_this\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def __init__(self):\n",
    "        self.count = 0\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        for i in range(len(grid)):\n",
    "            if grid[i][0] == 1:\n",
    "                self.dfs(grid, i, 0)\n",
    "            if grid[i][len(grid[0]) - 1] == 1:\n",
    "                self.dfs(grid, i, len(grid[0]) - 1)\n",
    "        for j in range(1, len(grid[0])-1):\n",
    "            if grid[0][j] == 1:\n",
    "                self.dfs(grid, 0, j)\n",
    "            if grid[len(grid) - 1][j] == 1:\n",
    "                self.dfs(grid, len(grid) - 1, j)\n",
    "        self.count = 0\n",
    "        for i in range(len(grid) - 1):\n",
    "            for j in range(len(grid[0]) - 1):\n",
    "                if grid[i][j] == 1:\n",
    "                    self.dfs(grid, i, j)\n",
    "        return self.count\n",
    "    def dfs(self, grid, x, y):\n",
    "        if grid[x][y] == 0:\n",
    "            return\n",
    "        grid[x][y] = 0\n",
    "        self.count += 1\n",
    "        dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]\n",
    "        for d in dirs:\n",
    "            nextx = x + d[0]\n",
    "            nexty = y + d[1]\n",
    "            if nextx < 0 or nexty < 0 or nextx >= len(grid) or nexty >= len(grid[0]):\n",
    "                continue\n",
    "            self.dfs(grid, nextx, nexty)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "\n",
    "        m, n, ans = len(grid), len(grid[0]), 0\n",
    "        def dfs(x, y):\n",
    "            num = 1\n",
    "            grid[x][y] = 0\n",
    "            direct = [(1, 0), (-1, 0), (0, 1), (0, -1)]\n",
    "            for i, j in direct:\n",
    "                if 0<=x+i<m and 0<=y+j<n and grid[x+i][y+j] == 1:\n",
    "                    num += dfs(x+i, y+j)\n",
    "            return num\n",
    "        \n",
    "        for i in [0, m-1]:\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 1:\n",
    "                    dfs(i, j)\n",
    "        for i in range(m):\n",
    "            for j in [0, n-1]:\n",
    "                if grid[i][j] == 1:\n",
    "                    dfs(i, j)\n",
    "        for i in range(1, m-1):\n",
    "            for j in range(1, n-1):\n",
    "                if grid[i][j] == 1:\n",
    "                    ans += dfs(i, j)\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "\n",
    "\n",
    "        m=len(grid)\n",
    "        n=len(grid[0])\n",
    "\n",
    "        visit= [[0 for i in range(n)] for j in range(m)]\n",
    "\n",
    "        def dfs(i,j):\n",
    "            if i<0 or i>=m or j<0 or j>=n:\n",
    "                return\n",
    "            if grid[i][j]==0:\n",
    "                return\n",
    "            \n",
    "            if visit[i][j]==1:\n",
    "                return\n",
    "            \n",
    "            visit[i][j]=1\n",
    "\n",
    "            direction =[(0,-1),(0,1),(-1,0),(1,0)]\n",
    "\n",
    "            for x,y in direction:\n",
    "                dfs(i+x,j+y)\n",
    "        \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if i==0 or i==m-1 or j==0 or j==n-1:\n",
    "                    dfs(i,j)\n",
    "        \n",
    "        # print(visit)\n",
    "\n",
    "        res=0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j]==1 and visit[i][j]==0:\n",
    "                    res+=1\n",
    "        return res\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        self.res = set()\n",
    "        self.grid = grid\n",
    "        sum_grid = sum([sum(row) for row in grid])\n",
    "\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid[0])):\n",
    "                if i == 0 or j == 0 or i == len(grid)-1 or j == len(grid[0])-1:\n",
    "                    if grid[i][j] == 1:\n",
    "                        if (i, j) not in self.res:\n",
    "                            self.res.add((i, j))\n",
    "                            self.dfs((i, j), len(grid), len(grid[0]))\n",
    "        print(sum_grid)\n",
    "        print(self.res)\n",
    "        return sum_grid - len(self.res)\n",
    "    \n",
    "    def dfs(self, points, m, n):\n",
    "        for op in [(0, 1), (1, 0), (0, -1), (-1, 0)]:\n",
    "            row, col = points[0] + op[0], points[1] + op[1]\n",
    "            \n",
    "            if row < 0 or row >= m or col < 0 or col >= n:\n",
    "                continue\n",
    "            if self.grid[row][col] == 0:\n",
    "                continue\n",
    "            if (row, col) not in self.res:\n",
    "                self.res.add((row, col))\n",
    "                self.dfs((row, col), m, n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        def dfs(i, j):\n",
    "            if 0 <= i < n and 0 <= j < m and grid[i][j] == 1 and not visited[(i, j)]:\n",
    "                visited[(i, j)] = True\n",
    "                for l, r in [(0, 1), (1, 0), (-1, 0), (0, -1)]:\n",
    "                    dfs(i+l, j+r)\n",
    "\n",
    "        n, m = len(grid), len(grid[-1])\n",
    "        visited = defaultdict(lambda: False)\n",
    "        ret = 0\n",
    "        for i in range(n):\n",
    "            dfs(i, 0)\n",
    "            dfs(i, m-1)\n",
    "        for i in range(m):\n",
    "            dfs(0, i)\n",
    "            dfs(n-1, i)\n",
    "        ret = sum([sum(g) for g in grid]) - sum(visited.values())\n",
    "        # return ret\n",
    "        return sum(grid[i][j] and not visited[(i,j)] for i in range(1, n - 1) for j in range(1, m - 1))             \n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        def dfs(grid, visited, x, y):\n",
    "            dx = [0, 0, 1, -1]\n",
    "            dy = [1, -1, 0, 0]\n",
    "            for i in range(4):\n",
    "                nx = x + dx[i]\n",
    "                ny = y + dy[i]\n",
    "                if nx<0 or ny<0 or nx>=len(grid) or ny>=len(grid[0]):\n",
    "                    continue\n",
    "                if grid[nx][ny] == 0 or visited[nx][ny]:\n",
    "                    continue\n",
    "                visited[nx][ny] = True\n",
    "                dfs(grid, visited, nx, ny)\n",
    "        \n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        visited = [[False]*n for _ in range(m)]\n",
    "        cnt = 0\n",
    "        # 判断左右边界 每一个1是否可以到达边界\n",
    "        for i in range(m):\n",
    "            if grid[i][0] ==1:\n",
    "                visited[i][0] = True\n",
    "                dfs(grid, visited, i, 0)\n",
    "            if grid[i][n-1]==1:\n",
    "                visited[i][n-1] = True\n",
    "                dfs(grid, visited, i, n-1)\n",
    "        \n",
    "        # 判断上下边界(顶点处不需要重复判断)\n",
    "        for j in range(1, n-1):\n",
    "            if grid[0][j] == 1:\n",
    "                visited[0][j] = True\n",
    "                dfs(grid, visited, 0, j)\n",
    "            if grid[m-1][j] == 1:\n",
    "                visited[m-1][j] = True\n",
    "                dfs(grid, visited, m-1, j)\n",
    "        \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 1 and not visited[i][j]:\n",
    "                    cnt+=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        def dfs(grid,i,j):\n",
    "            grid[i][j]=0\n",
    "            for x,y in [(i-1,j),(i+1,j),(i,j+1),(i,j-1)]:\n",
    "                if 0<=x<len(grid) and 0<=y<len(grid[0]) and grid[x][y]==1:\n",
    "                    dfs(grid,x,y)\n",
    "        for j in range(len(grid[0])):\n",
    "            if grid[0][j]==1:\n",
    "                dfs(grid,0,j)\n",
    "            if grid[len(grid)-1][j]==1:\n",
    "                dfs(grid,len(grid)-1,j)\n",
    "        for i in range(len(grid)):\n",
    "            if grid[i][0]==1:\n",
    "                dfs(grid,i,0)\n",
    "            if grid[i][len(grid[0])-1]==1:\n",
    "                dfs(grid,i,len(grid[0])-1)\n",
    "        total =0\n",
    "        for i in range(len(grid)):\n",
    "            total +=sum(grid[i])\n",
    "        return total \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        visited = set()\n",
    "\n",
    "        res = []\n",
    "        dirs = [(1,0),(-1,0),(0,-1),(0,1)]\n",
    "\n",
    "\n",
    "        def dfs(x,y,path):\n",
    "            if x > m - 1 or x < 0 or y > n - 1 or y < 0 or grid[x][y] == 0:\n",
    "                return\n",
    "            path.add((x,y))\n",
    "\n",
    "            visited.add((x,y))\n",
    "            for d in dirs:\n",
    "                nextx = x + d[0]\n",
    "                nexty = y + d[1]\n",
    "                if 0 <= nextx < m and 0 <= nexty < n and grid[nextx][nexty] == 1 and (nextx,nexty) not in visited:\n",
    "                    dfs(nextx,nexty,path)\n",
    "            return path\n",
    "\n",
    "        res = []\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j] == 1 and (i,j) not in visited:\n",
    "                    path = set()\n",
    "                    res.append(dfs(i,j,path)) \n",
    "\n",
    "        result = 0\n",
    "        for each in res:\n",
    "            for i in each:\n",
    "                if i[0] == 0 or i[0] == m - 1 or i[1] == 0 or i[1] == n - 1:\n",
    "                    aim = 0\n",
    "                    break\n",
    "                else:\n",
    "                    aim = len(each)\n",
    "            result += aim\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        ans=0\n",
    "        for i in range(len(grid)):\n",
    "            if grid[i][0]==1:\n",
    "                grid[i][0]=0\n",
    "                self.traverse_first(grid,i,0)\n",
    "            if grid[i][len(grid[0])-1]==1:\n",
    "                grid[i][len(grid[0])-1]=0\n",
    "                self.traverse_first(grid,i,len(grid[0])-1)\n",
    "        for i in range(len(grid[0])):\n",
    "            if grid[0][i]==1:\n",
    "                grid[0][i]=0\n",
    "                self.traverse_first(grid,0,i)\n",
    "            if grid[len(grid)-1][i]==1:\n",
    "                grid[len(grid)-1][i]=0\n",
    "                self.traverse_first(grid,len(grid)-1,i)\n",
    "        for i in range(len(grid)):\n",
    "            for j in range(len(grid[0])):\n",
    "                if grid[i][j]==1:\n",
    "                    ans+=1\n",
    "        return ans\n",
    "\n",
    "    def traverse_first(self,grid,i,j):\n",
    "        for next_i,next_j in [(i+1,j),(i-1,j),(i,j+1),(i,j-1)]:\n",
    "            if next_i<0 or next_i>len(grid)-1 or next_j<0 or next_j>len(grid[0])-1 or grid[next_i][next_j]==0:\n",
    "                continue\n",
    "            grid[next_i][next_j]=0\n",
    "            self.traverse_first(grid,next_i,next_j)\n",
    "\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def dfs(self,grid,row,col):\n",
    "        grid[row][col]=0\n",
    "        for x,y in [(row,col+1),(row,col-1),(row+1,col),(row-1,col)]:\n",
    "            if 0<=x<len(grid) and 0<=y<len(grid[0]) and grid[x][y]==1:\n",
    "                self.dfs(grid,x,y)\n",
    "\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        m=len(grid)\n",
    "        n=len(grid[0])\n",
    "        ans=0\n",
    "        for i in range(m):\n",
    "                if grid[i][0]==1:\n",
    "                    self.dfs(grid,i,0)\n",
    "                if grid[i][n-1]==1:\n",
    "                    self.dfs(grid,i,n-1)\n",
    "\n",
    "        for i in range(1,n-1):\n",
    "            if grid[0][i]==1:\n",
    "                self.dfs(grid,0,i)\n",
    "            if grid[m-1][i]==1:\n",
    "                self.dfs(grid,m-1,i)    \n",
    "\n",
    "        for row in range(m):\n",
    "            for col in range(n):\n",
    "                if grid[row][col] == 1:\n",
    "                    ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        #2: not accessible land\n",
    "        ans = 0\n",
    "        m, n = len(grid), len(grid[0])\n",
    "        directions = [(0,1), (1,0), (-1,0), (0,-1)]\n",
    "\n",
    "        def union(i, j, lands):\n",
    "            access = False\n",
    "            lands.add((i,j))\n",
    "            steps = []\n",
    "            for di, dj in directions:\n",
    "                if i+di<0 or i+di>m-1 or j+dj<0 or j+dj>n-1: access = True\n",
    "                elif grid[i+di][j+dj]==1 and ((i+di,j+dj) not in lands): steps.append((i+di, j+dj))\n",
    "\n",
    "            for next_i, next_j in steps:\n",
    "                access |= union(next_i, next_j, lands)\n",
    "            grid[i][j] = 0 if access else 2\n",
    "\n",
    "            return access\n",
    "            \n",
    "        \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j]==1:\n",
    "                    theland = set()\n",
    "                    if not union(i, j, theland): ans+=len(theland)\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        n, m = len(grid), len(grid[0])\n",
    "        def dfs(x, y):\n",
    "            if grid[x][y] == 0: return\n",
    "            grid[x][y] = 0\n",
    "            for a, b in [(x-1, y), (x+1, y), (x, y+1), (x, y-1)]:\n",
    "                if 0<=a<n and 0<=b<m:\n",
    "                    dfs(a, b)\n",
    "        for i in range(n):\n",
    "            dfs(i, 0)\n",
    "            dfs(i, m-1)\n",
    "        for j in range(m):\n",
    "            dfs(0, j)\n",
    "            dfs(n-1, j)\n",
    "        return sum(sum(x for x in r) for r in grid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        vis = [[False] * n for _ in range(m)]\n",
    "        bound = [(0, i) for i in range(n)] + [(m-1, i) for i in range(n)] + [(i, 0) for i in range(1, m-1)] + [(i, n-1) for i in range(1, m-1)]\n",
    "\n",
    "        def dfs(r, c):\n",
    "            if r < 0 or c < 0 or r >= len(grid) or c >= len(grid[0]) or grid[r][c] == 0 or vis[r][c]:\n",
    "                return\n",
    "            # grid[r][c] = 2\n",
    "            vis[r][c] = True\n",
    "            for x, y in ((r - 1, c), (r, c + 1), (r + 1, c), (r, c - 1)):\n",
    "                dfs(x, y)\n",
    "\n",
    "        for nx, ny in bound:\n",
    "            dfs(nx, ny)\n",
    "        # for i in range(m):\n",
    "        #     dfs(i, 0)\n",
    "        #     dfs(i, n - 1)\n",
    "        # for j in range(1, n - 1):\n",
    "        #     dfs(0, j)\n",
    "        #     dfs(m - 1, j)\n",
    "\n",
    "        # count = 0\n",
    "        # for i in range(1, m-1):\n",
    "        #     for j in range(1, n-1):\n",
    "        #         if grid[i][j] == 1:\n",
    "        #             count += 1        \n",
    "        return sum(grid[i][j] and not vis[i][j] for i in range(1, m - 1) for j in range(1, n - 1))\n",
    "\n",
    "# class Solution:\n",
    "#     def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "#         m, n = len(grid), len(grid[0])\n",
    "#         vis = [[False] * n for _ in range(m)]\n",
    "\n",
    "#         def dfs(r: int, c: int) -> None:\n",
    "#             if r < 0 or r >= m or c < 0 or c >= n or grid[r][c] == 0 or vis[r][c]:\n",
    "#                 return\n",
    "#             vis[r][c] = True\n",
    "#             for x, y in ((r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)):\n",
    "#                 dfs(x, y)\n",
    "\n",
    "#         for i in range(m):\n",
    "#             dfs(i, 0)\n",
    "#             dfs(i, n - 1)\n",
    "#         for j in range(1, n - 1):\n",
    "#             dfs(0, j)\n",
    "#             dfs(m - 1, j)\n",
    "#         return sum(grid[i][j] and not vis[i][j] for i in range(1, m - 1) for j in range(1, n - 1))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numEnclaves(self, grid: List[List[int]]) -> int:\n",
    "        def dfs(grid, i, j):\n",
    "            dirs = [[0,1],[0,-1],[1,0],[-1,0]]\n",
    "            m = len(grid)\n",
    "            n = len(grid[0])\n",
    "            if i< 0 or j<0 or i>=m or j>=n:\n",
    "                return\n",
    "            if grid[i][j]==0:\n",
    "                return\n",
    "            grid[i][j] = 0\n",
    "            for d in dirs:\n",
    "                new_i = i+d[0]\n",
    "                new_j = j+d[1]\n",
    "                dfs(grid, new_i, new_j)\n",
    "        m = len(grid)\n",
    "        n = len(grid[0])\n",
    "        res = 0\n",
    "        for i in range(m):\n",
    "            dfs(grid, i, 0)\n",
    "            dfs(grid, i, n-1)\n",
    "        for j in range(n):\n",
    "            dfs(grid, 0, j)\n",
    "            dfs(grid, m - 1, j)\n",
    "        print(grid)\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if grid[i][j]==1:\n",
    "                    res+=1\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
